﻿/* jquery.modalPopup-1.0.0.js" */
(function ($) {
    var popupStates = {
        closed: 0,
        opened: 1
    };

    var repositionMode = {
        none: 0,
        resize: 1,
        scroll: 2,
        resizescroll: 3
    };

    var methods = {
        init: function (options) {
            this.each(function () {
                if ($("#modalPopupWrapper").length == 0) {
                    $("<div/>")
                        .attr("id", "modalPopupWrapper")
                        .css("display", "none")
                        .addClass("jqube-modalpopup-wrapper")
                        .appendTo($("form"));

                    $("<div>")
                        .attr("id", "modalPopupBackground")
                        .css("display", "none")
                        .css("opacity", 0)
                        .css("position", "absolute")
                        .addClass(options.backgroundClass)
                        .width($(document).width())
                        .height($(document).height())
                        .click(methods.backgroundClick)
                        .appendTo("#modalPopupWrapper");
                }

                var dataWrapper = $("#modalPopupWrapper").data("modalpopup-data");

                if (!dataWrapper) {
                    dataWrapper = {
                        modals: []
                    };
                }

                var modal = {
                    popup: $(this),
                    trigger: null,
                    state: popupStates.closed,
                    options: options
                };

                var popupWidth = $(this).width() + 10;
                var popupHeight = $(this).height() + 35;

                if (options.title && options.title != "") {
                    $("<div/>")
                        .attr("id", "title")
                        .html(options.title)
                        .insertBefore($(this).find("div:first-child"));
                }

                if (options.enabledOkButton || options.enabledCancelButton) {
                    $(this).find("div:last-child").css("bottom", 40);
                    popupHeight += 40;

                    var buttonWrapper = $("<div/>")
                        .addClass("jqube-modalpopup-popup-buttons")
                        .appendTo($(this));

                    if (options.enabledOkButton) {
                        buttonWrapper.append(
                            $("<input/>")
                                .attr("id", $(this).attr("id") + "OkButton")
                                .attr("type", "button")
                                .width(70)
                                .height(25)
                                .css("margin-top", 5)
                                .val(options.okButtonText)
                                .click(methods.onOkClick)
                                .data("modalpopup-data", { modal: modal }));
                    }

                    if (options.enabledCancelButton) {
                        buttonWrapper.append(
                            $("<input/>")
                                .attr("id", $(this).attr("id") + "CancelButton")
                                .attr("type", "button")
                                .width(70)
                                .height(25)
                                .css("margin-top", 5)
                                .css("margin-left", 5)
                                .val(options.cancelButtonText)
                                .click(methods.onCancelClick)
                                .data("modalpopup-data", { modal: modal }));
                    }
                }

                $(this)
                    .css("position", "absolute")
                    .css("display", "none")
                    .css("opacity", 0)
                    .width(popupWidth)
                    .height(popupHeight)
                    .appendTo("#modalPopupWrapper");

                var popupBorder = $("<div/>")
                    .addClass(options.popupBorderCssClass);

                $(this).wrapInner(popupBorder);

                if (options.triggerControlID && options.triggerControlID != "") {
                    modal.trigger = $("#" + options.triggerControlID);
                    modal.trigger.data("modalpopup-data", { modal: modal });
                    modal.trigger.click(methods.open);
                }

                if (options.repositionMode) {
                    switch (options.repositionMode) {
                        case repositionMode.resize:
                            $(window).resize(methods.reposition);
                            break;
                        case repositionMode.scroll:
                            $(window).scroll(methods.reposition);
                            break;
                        case repositionMode.resizescroll:
                            $(window).resize(methods.reposition);
                            $(window).scroll(methods.reposition);
                            break;
                        default:
                            break;
                    }
                }

                if (options.closeOnEscapeKey)
                    $(document).keyup(methods.keyUp);



                dataWrapper.modals.push(modal);

                $("#modalPopupWrapper").data("modalpopup-data", dataWrapper);
            });
        },
        destroy: function () {

        },
        onOkClick: function () {
            var modal = $(this).data("modalpopup-data").modal;

            methods.close(modal);

            if ($.isFunction(modal.options.onCancel)) {
                modal.options.onCancel();
            }

        },
        onCancelClick: function () {
            var modal = $(this).data("modalpopup-data").modal;
            
            methods.close(modal);

            if ($.isFunction(modal.options.onCancel)) {
                modal.options.onCancel();
            }
        },
        reposition: function () {
            var modal = $("#modalPopupWrapper").data("modalpopup-data");

            $.each(modal.modals, function (index, modal) {
                if (modal.state == popupStates.opened) {
                    var popupOffset = modal.popup.data("modalpopup-offset");

                    var scrollTop = $(window).scrollTop();
                    var scrollLeft = $(window).scrollLeft();

                    var popupTop = 0;
                    var popupLeft = 0;

                    switch (modal.options.repositionMode) {
                        case repositionMode.resize:
                            popupTop = ($(window).height() / 2) - (modal.popup.height() / 2);
                            popupLeft = ($(window).width() / 2) - (modal.popup.width() / 2);
                            break;
                        case repositionMode.scroll:
                            popupTop = popupOffset.top + scrollTop;
                            popupLeft = popupOffset.left + scrollLeft;
                            break;
                        default:
                            popupTop = ($(window).height() / 2) - (modal.popup.height() / 2) + scrollTop;
                            popupLeft = ($(window).width() / 2) - (modal.popup.width() / 2) + scrollLeft;
                            break;
                    }

                    modal.popup.animate({
                        top: popupTop,
                        left: popupLeft
                    }, 10);
                }
            });
        },
        close: function (modal) {
            if (modal.state == popupStates.opened) {
                $("#modalPopupWrapper").css("display", "none");
                $("#modalPopupBackground").css("display", "none");
                modal.popup.css("display", "none");
                modal.state = popupStates.closed;
            }

            return false;
        },
        keyUp: function (event) {
            var modal = $("#modalPopupWrapper").data("modalpopup-data");

            $.each(modal.modals, function (index, modal) {
                if (modal.state == popupStates.opened && event.keyCode == 27) {
                    methods.close(modal);
                }
            });
        },
        backgroundClick: function () {
            var modal = $("#modalPopupWrapper").data("modalpopup-data");

            $.each(modal.modals, function (index, modal) {
                if (modal.state == popupStates.opened && modal.options.closeOnBackgroundClick) {
                    methods.close(modal);
                }
            });
        },
        open: function () {
            var modal = $(this).data("modalpopup-data").modal;

            if (modal.state == popupStates.closed) {
                $("#modalPopupWrapper").css("display", "block");

                $("#modalPopupBackground")
                    .css("display", "block")
                    .fadeTo('fast', .4, function () {
                        modal.popup
                        .css("display", "block")
                        .fadeTo('fast', 1)
                        .offset({
                            top: ($(window).height() / 2) - (modal.popup.height() / 2),
                            left: ($(window).width() / 2) - (modal.popup.width() / 2)
                        });
                    });

                modal.popup.data("modalpopup-offset", modal.popup.offset());

                modal.state = popupStates.opened;
            }

            return false;
        }
    };

    $.fn.modalPopup = function (method, options) {


        var settings = $.extend({
            backgroundClass: "jqube-modalpopup-background",
            popupBorderCssClass: "jqube-modalpopup-popup_border",
            triggerControlID: null,
            repositionMode: repositionMode.resizescroll,
            closeOnBackgroundClick: false,
            closeOnEscapeKey: false,
            title: "",
            enabledOkButton: true,
            okButtonText: "OK",
            enabledCancelButton: true,
            cancelButtonText: "Cancel"
        }, options);

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.modalPopup');
        }
    };
})(jQuery);